package com.woniu.Service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.woniu.Service.RoleService;
import com.woniu.entity.Role;
import com.woniu.mapper.RoleMapper;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * @Author: Gwj
 * @Date: 2024/7/3 19:28
 */
@Service
@Transactional
public class RoleServiceImpl implements RoleService {
    @Resource
    private RoleMapper roleMapper;

    /**
     * 获取所有的角色信息，用于对用户的授权
     *
     * @return
     * @throws Exception
     */
    @Override
    @Cacheable(value = "role")
    public List<Role> getAllRoleInfo(Integer isDelete) throws Exception {
        Example example = new Example(Role.class);
        Example.Criteria c = example.createCriteria();
        c.andEqualTo("isDelete",0);
        return roleMapper.selectByExample(example);
    }


    /**
     * 角色查询，分页查询
     *
     * @param pageNum
     * @param pageSize
     * @return
     * @throws Exception
     */
    @Override
    @Cacheable(value = "role", key = "#root.methodName+#root.args[0]+#root.args[1]")
    public PageInfo<Role> findRoleInfoByPage(int pageNum, int pageSize) throws Exception {
        Page<Role> page = PageHelper.startPage(pageNum, pageSize);
        roleMapper.selectAll();
        return page.toPageInfo();
    }

    /**
     * 删除角色 逻辑删除 isDelete字段在自动生成时是 布尔型 手动改成 int型
     *
     * @param role
     * @return
     * @throws Exception
     */
    @Override
    @CacheEvict(value = "role", allEntries = true)
    public int changeRoleIsDeleted(Role role) throws Exception {
        role.setIsDelete(1);
        return roleMapper.updateByPrimaryKeySelective(role);
    }

    /**
     * 角色新增
     *
     * @param role
     * @return
     * @throws Exception
     */
    @Override
    @CacheEvict(value = "role", allEntries = true)
    public int addRole(Role role) throws Exception {
        return roleMapper.insertSelective(role);
    }

    /**
     * 角色信息修改
     *
     * @param role
     * @return
     * @throws Exception
     */
    @Override
    @CacheEvict(value = "role", allEntries = true)
    public int updateRoleInfoById(Role role) throws Exception {
        return roleMapper.updateByPrimaryKeySelective(role);
    }

    /**
     * 角色新增时的重名验证
     *
     * @param roleName
     * @return
     * @throws Exception
     */
    @Override
    public Role checkRoleName(String roleName) throws Exception {
        Example example = new Example(Role.class);
        Example.Criteria c = example.createCriteria();
        c.andEqualTo("roleName", roleName);
        return roleMapper.selectOneByExample(example);
    }

    /**
     * 角色修改时的重名验证
     *
     * @param roleName
     * @param roleId
     * @return
     * @throws Exception
     */
    @Override
    public Role selectRoleByNameAndId(String roleName, Integer roleId) throws Exception {
        return roleMapper.selectRoleByNameAndId(roleName, roleId);
    }

    /**
     * 角色根据名字模糊查询 ，分页查询
     *
     * @param roleName
     * @param pageNum
     * @param pageSize
     * @return
     * @throws Exception
     */
    @Override
    public PageInfo<Role> selectRoleInfoByRoleName(String roleName, int pageNum, int pageSize) throws Exception {
        Page<Role> page = PageHelper.startPage(pageNum, pageSize);
        Example example = new Example(Role.class);
        Example.Criteria c = example.createCriteria();
        c.andLike("roleName", "%" + roleName + "%");
        roleMapper.selectByExample(example);
        return page.toPageInfo();
    }
}
